﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Studio77.CrazyJigsaw.Entities;
using System.Windows.Navigation;
using Studio77.CrazyJigsaw.Pages;
using Studio77.CrazyJigsaw.Core;
using Studio77.CrazyJigsaw.Util;

namespace Studio77.CrazyJigsaw.Views
{
    public partial class StageItemView : UserControl
    {
        int index;

        public StageItemView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(StageItemView_Loaded);
            this.Tap += new EventHandler<GestureEventArgs>(StageItemView_Tap);
        }

        void StageItemView_Tap(object sender, GestureEventArgs e)
        {
            //点击，进入关卡
            this.StoryboardClick.Begin();
            this.StoryboardClick.Completed += new EventHandler(StoryboardClick_Completed);
            //播放音效
            SoundManager.PlayEffect(1);
        }

        void StoryboardClick_Completed(object sender, EventArgs e)
        {
            //设置LastStage
            String stageCode = ((StageItemViewModel)this.DataContext).StageType + "_" + index.ToString();
            StageManager.Instance.LastStage = stageCode;

            StagePage.CurrentStagePage.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?StageCode=" + stageCode, UriKind.Relative));
        }

        void StageItemView_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(StageItemView_Loaded);
            //播放动画
            index = ((StageItemViewModel)this.DataContext).Index;
            this.StoryboardShowing.BeginTime = TimeSpan.FromMilliseconds((index + 1) * 50);
            this.StoryboardShowing.Begin();
        }
    }
}
